package com.general.framework.zzyy.thread03;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeUnit;

/**
 * @Description: thenCombine 合并多个CompletableFuture的结果
 * @Author：xiaoqi
 * @creat 2023/8/6 11:53
 */
public class CombineCompletableFutureResultDemo {
    public static void main(String[] args) {
        CompletableFuture<Integer> completableFuture1 = CompletableFuture.supplyAsync(() -> {
            System.out.println(Thread.currentThread().getName() + " 启动-->模拟 HttpClient 耗时操作 1");
            try {
                TimeUnit.SECONDS.sleep(1);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            return 10;
        });

        CompletableFuture<Integer> completableFuture2 = CompletableFuture.supplyAsync(() -> {
            System.out.println(Thread.currentThread().getName() + " 启动-->模拟 HttpClient 耗时操作 2");
            try {
                TimeUnit.SECONDS.sleep(2);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            return 20;
        });

        CompletableFuture<Integer> future = completableFuture1.thenCombine(completableFuture2, (a, b) -> {
            System.out.println("开始合并");
            return a + b;
        });
        System.out.println(future.join());

    }
}
/**
 * 运行结果：
 * ForkJoinPool.commonPool-worker-1 启动
 * ForkJoinPool.commonPool-worker-2 启动
 * 开始合并
 * 30
 */
